package com.ruoyi.resume.utils.zhipu;

import com.alibaba.fastjson2.JSON;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Glm {
    public static final String API_KEY_Z = "98b61ac3fe74307b7549915437425356.xmyraauKeGEOTaZP";
    public static final String API_KEY_T ="c660c1696e4540fd96a9a54f27e59db0.XYa4hdYg0XYTQd1B";
//    public static final String API_KEY ="52dc4920eaca784921456fb698088d9e.ZKYIaLFQ3pNqwgWO";
    public static final String API_KEY ="0578c14c352d4249b5cf965b3e846d50.NgFpHv2w5b83dz9G";

    private static final String requestIdTemplate = "miitang-%d";

    public static String chatGLM4(String message) {
        // 创建客户端
        ClientV4 client = new ClientV4.Builder(API_KEY)
                .networkConfig(90,180,180,180, TimeUnit.SECONDS)
                .build();

        // 构建消息对象
        List<ChatMessage> messages = new ArrayList<>();
        ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), message);
        messages.add(chatMessage);

        // 构建请求id
        String requestId = String.format(requestIdTemplate, System.currentTimeMillis());

        // 创建请求对象
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model("glm-4-plus") // 设置模式
                .stream(Boolean.FALSE)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .requestId(requestId)
                .build();

        // 调用模型API
        ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);

        // 将API响应转换为JSON字符串
        String responseJson = JSON.toJSONString(invokeModelApiResp);

        // 提取并返回对话内容
        return extractMessageContent(responseJson);
    }

    private static String extractMessageContent(String responseJson) {
        // 解析响应JSON
        JSONObject responseObject = JSON.parseObject(responseJson);

        // 获取 "data" -> "choices" -> "message" -> "content" 字段的值
        JSONObject data = responseObject.getJSONObject("data");
        if (data != null) {
            List<JSONObject> choices = data.getJSONArray("choices").toList(JSONObject.class);
            if (!choices.isEmpty()) {
                JSONObject message = choices.get(0).getJSONObject("message");
                if (message != null) {
                    return message.getString("content"); // 提取并返回 "content" 字段
                }
            }
        }

        // 如果没有找到内容，返回一个空字符串
        return "";
    }

    public static void main(String[] args) {
        String result = chatGLM4("你好，请锐评下java工程师");
        System.out.println("提取的内容: " + result);
    }
}
